package com.djhu.freepartner.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@ConfigurationProperties
@EnableConfigurationProperties(value = {MailConfig.class})
@ConditionalOnProperty(prefix = "spring.freepartner.mail", value="enabled", matchIfMissing =false )
public class MailConfiguration {

    @Autowired
    private MailConfig mailConfig;

    @Bean
    public MailSender mailSender(){
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(mailConfig.getHost());//指定用来发送Email的邮件服务器主机名
        mailSender.setPort(mailConfig.getPort());//默认端口，标准的SMTP端口
        mailSender.setUsername(mailConfig.getUsername());//用户名
        mailSender.setPassword(mailConfig.getPassword());//密码
        return mailSender;
    }
}
